const assert = require("assert");
const Db = require("../src/db.js");
const Menu = require("./data/menu.js");
const Product = require("./data/product.js");
const Test = require("./data/test.js");

describe("db.js", () => {
  before(() => {
    console.log("init");
    Db.init("127.0.0.1", "sheng-test", "dev", "^wXj&CJQfiPP");
  });
  describe("getInstance()", () => {
    it("connect", async () => {
      try {
        await Db.getInstance().authenticate();
        console.log("Connection has been established successfully.");
        assert.ok(true);
      } catch (error) {
        console.error("Unable to connect to the database:", error);
        assert.ok(false);
      }
    });
  });
  describe("add()", () => {
    it("success", () => {
      Db.add(Menu, { name: "test name", ordering: "1" });
    });
    it("double key", (done) => {
      Db.add(Product, {
        code: "test",
        name: "产品产品",
        img: "https://sheng-production.oss-cn-shenzhen.aliyuncs.com/product/naixue20210429-8.jpg",
        desc: "小程序绑定使用。可自提可外卖",
        menuId: 10,
        marketPrice: 30.2,
        status: 1,
        ordering: 0,
      })
        .then(() => done())
        .catch((e) => {
          if (e.message.indexOf("不可重复") >= 0) {
            done();
          } else {
            done("没有捕捉错误");
          }
        });
    });
  });
  describe("update()", () => {
    it("success", () => {
      Db.update(Menu, { name: "test name2", ordering: "2" }, { id: 7 });
    });
  });
  describe("delOne()", () => {
    it("success", () => {
      Db.delOne(Menu, { code: "caidan" });
    });
    it("no product", async () => {
      try {
        await Db.delOne(Menu, { id: 999 });
        throw new Error("没有处理错误");
      } catch (e) {
        console.log(e.message);
        return;
      }
    });
  });
  describe("find()", () => {
    it("success", async () => {
      let data = await Db.find(Menu, { ordering: "1" });
      console.log(data);
      assert(data.count > 0, "no data");
    });
    it("page 2", async () => {
      let data = await Db.find(Menu, { ordering: "2" }, [["id", "desc"]], 2, 3);
      console.log(data);
      assert(data.count > 0, "no data");
    });
    it("order result", async () => {
      let data = await Db.find(Menu, { ordering: "2" }, [
        ["createTime", "desc"],
      ]);
      console.log(data);
      assert(data.count > 0, "no data");
    });
    it("select column", async () => {
      let data = await Db.find(
        Menu,
        { ordering: "0" },
        [["id", "desc"]],
        1,
        10,
        ["id"]
      );
      console.log(data);
      assert(data.count > 0, "no data");
    });
    it("before time", async () => {
      let data = await Db.find(
        Menu,
        { createTime: { [Db.Op.lte]: new Date() } },
        [["id", "desc"]]
      );
      console.log(data);
      assert(data.count > 0, "no data");
    });
  });
  describe("sql()", () => {
    it("no value", async () => {
      let data = await Db.sql("select * from menu");
      console.log(data);
    });
    it("have values", async () => {
      let data = await Db.sql("select * from menu where id=?", [1]);
      console.log(data);
    });
    it("to model", async () => {
      let data = await Db.sql(
        "select * from city",
        [],
        Db.SQL_TYPE_SELECT,
        true
      );
      console.log(JSON.stringify(data));
    });
    it("insert", async () => {
      let data = await Db.sql(
        "insert into menu (name) values(?)",
        ["test"],
        Db.SQL_TYPE_INSERT
      );
      console.log(data);
    });
    it("update", async () => {
      let data = await Db.sql(
        "update menu set name=? where name=?",
        ["test2", "test"],
        Db.SQL_TYPE_UPDATE
      );
      console.log(data);
    });
    it("delete", async () => {
      let data = await Db.sql(
        "delete from menu where name=?",
        ["test2"],
        Db.SQL_TYPE_DELETE
      );
      console.log(data);
    });
  });
  describe("findOne()", () => {
    it("success", async () => {
      let data = await Db.findOne(Menu, { ordering: 1 }, [["id", "desc"]]);
      console.log(data);
    });
    it("select column", async () => {
      let data = await Db.findOne(
        Menu,
        { ordering: 1 },
        [["id", "desc"]],
        ["id"]
      );
      console.log(data);
    });
  });
  describe("fullUpdate()", () => {
    it("success", async () => {
      let newData = [
        {
          name: "23",
          value: "3",
        },
      ];
      let oldData = await Db.find(Test);
      try {
        await Db.fullUpdate(Test, newData, oldData.rows, "name", true, [
          "value",
        ]);
      } catch (e) {
        console.error(e);
        assert(false);
      }
    });
  });

  describe("findAll()", () => {
    it("success", async () => {
      let data = await Db.findAll(Menu, { ordering: "1" });
      console.log(data);
      assert(data.length > 0, "no data");
    });
  });
});
